package com.server.questionbank.snframe.mapper.Course.Member;

import com.server.questionbank.snframe.domain.Course.Member.CusStudyGroup;
import org.apache.el.parser.BooleanNode;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MemberGroupMapper {
    // 插入一条新的用户学习组信息
    @Insert("INSERT INTO cus_study_group (course_id, group_name, is_default) VALUES(#{courseId}, #{groupName}, #{isDefault});")
    public int insertCusStudyGroup(@Param("courseId") Long courseId, @Param("groupName") String groupName, @Param("isDefault") String isDefault);

    // 查询指定学习组信息
    @Select("SELECT * FROM cus_study_group WHERE group_id = #{groupId}")
    public CusStudyGroup getCusStudyGroupById(@Param("groupId") Long groupId);

    // 查询指定 课程 的用户学习组信息
    @Select("SELECT * FROM cus_study_group WHERE course_id = #{courseId}")
    public List<CusStudyGroup> getCusStudyGroupByCourseId(@Param("courseId") Long courseId);


    // 查询指定 课程 的 默认 用户学习组信息
    @Select("<script> " +
            "    ( " +
            "        SELECT * FROM cus_study_group " +
            "        WHERE course_id = #{courseId} AND is_default = 'Y' " +
            "        LIMIT 1 " +
            "    ) " +
            "    UNION ALL " +
            "    ( " +
            "        SELECT * FROM cus_study_group " +
            "        WHERE course_id = #{courseId} " +
            "        ORDER BY ( " +
            "            SELECT COUNT(*) FROM user_regist_cus " +
            "            WHERE group_id = cus_study_group.group_id " +
            "        ) ASC " +
            "        LIMIT 1 " +
            "    ) " +
            "    LIMIT 1 " +
            "    </script>")
    CusStudyGroup getDefaultCusStudyGroupByCourseId(
            @Param("courseId") Long courseId
    );
}
